﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class ReceiptValidationBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly IReceiptValidationDA receiptValidationDA = DataAccess.CreateReceiptValidationDA();

        public ReceiptValidationBLL()
        {
            this.pubParasInfo = null;
        }

        public ReceiptValidationBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public ReceiptValidationBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> IDS)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string whLoginID = this.dataBaseID[0];
            string temp = string.Empty;
            AuditDataBLL auditDataBLL = new AuditDataBLL();
            try
            {
                List<ReceiptValidationInfo> equipmentProfileInfoList = new List<ReceiptValidationInfo>();
                ReceiptValidationInfo equipmentProfileInfo = null;
                foreach (string id in IDS)
                {
                    equipmentProfileInfo = receiptValidationDA.GetReceiptValidationByID(dataBase, tran, id, this.dataBaseID[0]);
                    equipmentProfileInfoList.Add(equipmentProfileInfo);
                }
                if (receiptValidationDA.DeleteReceiptValidation(dataBase, tran, IDS, this.dataBaseID[0]) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<ReceiptValidationInfo>(dataBase, tran, equipmentProfileInfoList[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "RECEIPT_VALIDATION", this.dataBaseID[0]);
                        temp = temp + IDS[i] + ",";
                    }
                }
                tran.Commit();
                temp = temp.Substring(0, temp.Length - 1);
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return receiptValidationDA.GetAllReceiptValidation(this.dataBaseID[0]);
        }

        public object GetInfoByID(string id)
        {
            return receiptValidationDA.GetReceiptValidationByID(id, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            ReceiptValidationQueryEntity receiptValidationQueryEntity = obj as ReceiptValidationQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(receiptValidationQueryEntity.ReceiptValidationID))
            {
                sqlWhere.Add(" RECEIPT_VALIDATION_ID " + receiptValidationQueryEntity.ReceiptValidationID);
            }
            if (!string.IsNullOrEmpty(receiptValidationQueryEntity.Descr))
            {
                sqlWhere.Add(" DESCR " + receiptValidationQueryEntity.Descr);
            }
            if (!string.IsNullOrEmpty(receiptValidationQueryEntity.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + receiptValidationQueryEntity.IsActive);
            }
            return receiptValidationDA.GetReceiptValidationByQueryList(sqlWhere, receiptValidationQueryEntity, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            ReceiptValidationInfo receiptValidationDAInfo = obj as ReceiptValidationInfo;
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                if (receiptValidationDA.CheckReceiptValidationIDUnique(dataBase, tran, receiptValidationDAInfo.ReceiptValidationID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2340_006", this.pubParasInfo.UserLanguage, new List<string> { receiptValidationDAInfo.ReceiptValidationID });
                }
                receiptValidationDAInfo.CreatedDate = DateTime.Now;
                receiptValidationDAInfo.UpdatedDate = DateTime.Now;
                receiptValidationDAInfo.UpdatedBy = receiptValidationDAInfo.CreatedBy;
                receiptValidationDA.InsertReceiptValidation(dataBase, tran, receiptValidationDAInfo, this.dataBaseID[0]);
                new AuditDataBLL().InsertAuditData<ReceiptValidationInfo>(dataBase, tran, null, receiptValidationDAInfo, AuditAction.Insert, this.pubParasInfo.UserID, "RECEIPT_VALIDATION", this.dataBaseID[0]);
                tran.Commit();
                temp = receiptValidationDAInfo.ReceiptValidationID;
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public string Update(object obj)
        {
            ReceiptValidationInfo receiptValidationDAInfo = obj as ReceiptValidationInfo;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                ReceiptValidationInfo oldReceiptValidationInfo = receiptValidationDA.GetReceiptValidationByID(dataBase, tran, receiptValidationDAInfo.ReceiptValidationID, this.dataBaseID[0]);
                receiptValidationDA.UpdateReceiptValidation(dataBase, tran, receiptValidationDAInfo, this.dataBaseID[0]);
                new AuditDataBLL().InsertAuditData<ReceiptValidationInfo>(oldReceiptValidationInfo, receiptValidationDAInfo, AuditAction.Update, this.pubParasInfo.UserID, "RECEIPT_VALIDATION", this.dataBaseID[0]);
                tran.Commit();
                return receiptValidationDAInfo.ReceiptValidationID;
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return "";
        }
    }
}

